package ai.people.platform.archives.intimacy.handler.impl;

import ai.people.core.grpc.lib.key.account.RedditResponse;
import ai.people.core.nebula.util.NebulaQueryUtils;
import ai.people.netmon.framework.domain.graph.enums.GraphTagEnum;
import ai.people.netmon.framework.domain.key.account.entity.intimacy.RedditIntimacy;
import ai.people.netmon.framework.domain.key.account.enums.IntimacyFieldsEnum;
import ai.people.netmon.framework.domain.key.account.enums.IntimacySupportEnum;
import ai.people.netmon.framework.domain.key.account.vo.ExtendVO;
import ai.people.platform.archives.intimacy.IntimacyHandlerFactory;
import ai.people.platform.archives.intimacy.handler.IntimacyHandleService;
import com.alibaba.fastjson.JSONObject;
import com.vesoft.nebula.client.graph.exception.IOErrorException;
import com.vesoft.nebula.client.graph.net.SessionWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhenglin
 * @date 2022/7/28
 */
@Service
@Slf4j
public class RedditIntimacyHandleServiceImpl implements IntimacyHandleService, InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        IntimacyHandlerFactory.registerIntimacyHandler(IntimacySupportEnum.REDDIT.value, this);
    }

    @Override
    public List<ExtendVO.Extend> analysis(List<?> responseList, Map<String, Integer> ruleMapping, String username, SessionWrapper session) throws IOErrorException, UnsupportedEncodingException {
        List<RedditResponse> data = (List<RedditResponse>) responseList;

        Set<String> vidList = data.stream().map(x -> NebulaQueryUtils.buildVid(GraphTagEnum.REDDIT.vidPrefix, x.getUsername())).collect(Collectors.toSet());
        Map<String, Integer> sameSectionMapping = getSameSectionMapping(session, vidList, username);

        List<RedditIntimacy> redditIntimacies = new ArrayList<>();
        for (RedditResponse response : data) {
            RedditIntimacy intimacy = JSONObject.parseObject(JSONObject.toJSONString(response), RedditIntimacy.class);
            intimacy.setUsername(response.getUsername());

            int sameSectionWight = getFieldWeight(IntimacyFieldsEnum.same_section_cnt.name(), ruleMapping);
            int followWight = getFieldWeight(IntimacyFieldsEnum.follow.name(), ruleMapping);
            int likeWight = getFieldWeight(IntimacyFieldsEnum.like_cnt.name(), ruleMapping);
            int notLikeWight = getFieldWeight(IntimacyFieldsEnum.not_like_cnt.name(), ruleMapping);
            int commentWight = getFieldWeight(IntimacyFieldsEnum.comment_cnt.name(), ruleMapping);
            int privateLetterWight = getFieldWeight(IntimacyFieldsEnum.private_letter_cnt.name(), ruleMapping);

            Integer sameSectionCnt = sameSectionMapping.getOrDefault(intimacy.getUsername(), 0);

            int qmd = sameSectionCnt * sameSectionWight + intimacy.getFollow() * followWight
                    + intimacy.getLikeCnt() * likeWight + intimacy.getNotLikeCnt() * notLikeWight + intimacy.getCommentCnt() * commentWight
                    + intimacy.getPrivateLetterCnt() * privateLetterWight;

            intimacy.setQmd(qmd);
            redditIntimacies.add(intimacy);
        }
        List<ExtendVO.Extend> res = new ArrayList<>();
        redditIntimacies.sort(Comparator.comparingInt(RedditIntimacy::getQmd).reversed());
        for (RedditIntimacy intimacy : redditIntimacies) {
            ExtendVO.Extend extendVO = new ExtendVO.Extend();
            extendVO.setUsername(intimacy.getUsername());
            extendVO.setQmd(intimacy.getQmd());

            int sameSectionCnt = intimacy.getSameSectionCnt();
            int follow = intimacy.getFollow();
            int likeCnt = intimacy.getLikeCnt();
            int notLikeCnt = intimacy.getNotLikeCnt();
            int commentCnt = intimacy.getCommentCnt();
            int privateLetterCnt = intimacy.getPrivateLetterCnt();

            JSONObject obj = new JSONObject();
            obj.put(IntimacyFieldsEnum.same_section_cnt.name(), sameSectionCnt);
            obj.put(IntimacyFieldsEnum.follow.name(), follow>0);
            obj.put(IntimacyFieldsEnum.like_cnt.name(), likeCnt);
            obj.put(IntimacyFieldsEnum.not_like_cnt.name(), notLikeCnt);
            obj.put(IntimacyFieldsEnum.comment_cnt.name(), commentCnt);
            obj.put(IntimacyFieldsEnum.private_letter_cnt.name(), privateLetterCnt);

            extendVO.setBasis(obj);
            res.add(extendVO);
        }
        return res;
    }


}
